package com.huluobo.a2109mvpimdemo.utls;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;

/**
 * Created by LC on 2023/11/24.
 */
public class NetUtils {
    //单例模式核心,私有化构造函数
    private NetUtils() {

    }

    private static NetUtils netUtils = null;

    private static Context mContext;

    public static synchronized NetUtils getInstance(Context context) {
        if (netUtils == null) {
            netUtils = new NetUtils();
        }
        mContext = context;
        return netUtils;
    }

    public boolean isConnected() {
        //获取网络连接管理器
        ConnectivityManager connectivityManager =
                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        //拿到当前的网络信息
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        //判断当前是有有网络信息,并且当前网络状态是已连接或者连接中
        return networkInfo != null && networkInfo.isConnectedOrConnecting();
    }

    public boolean isSlowlyNetWork() {
        //获取连接管理器
        ConnectivityManager connectivityManager =
                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        //获取网络流量能力管理器
        NetworkCapabilities networkCapabilities =
                connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());

        //判断当前网络装是蜂窝数据(2G 3G 4G 5G)
        if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
            //潘丹网络上下行速度是否都大于100kps
            if (networkCapabilities.getLinkUpstreamBandwidthKbps() > 100
                    && networkCapabilities.getLinkDownstreamBandwidthKbps() > 100) {
                //如果大于,证明不是弱网状态
                return false;
            } else {
                //否则,是弱网转
                return true;
            }
        }
        //默认返回是弱网状态
        return true;
    }
}
